package ch.bfh.fbi.mobicomp.zap02;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import ch.quantasy.android.speech.SpeechService;
import ch.quantasy.android.speech.string.UKString;

/**
 * We can even use some other services (Libraries) in order to enrich our
 * application.
 * 
 * @author quantasy
 * 
 */
public class Zap02MessageBroadcastReceiver extends BroadcastReceiver {
	public static final String REMOTE_ZAPPER_ACTION = "ch.bfh.fbi.mobicomp.zap02.Zap02MessageBroadcastReceiver.REMOTE_ZAPPER_ACTION";
	public static final String ZAPPER_KEY_WORD = "ZAP:";
	private static final String destructionKey = "destroyMe";
	private static final String haltKey = "stopDestroyMe";

	private static final String TAG = "ZAP02";
	private final Intent speechIntent;

	public static Intent getRemoteZapperMessageIntent(final String message) {
		Intent intent = null;
		if (message.startsWith(Zap02MessageBroadcastReceiver.ZAPPER_KEY_WORD)) {
			intent = new Intent(
					Zap02MessageBroadcastReceiver.REMOTE_ZAPPER_ACTION);
			intent.putExtra("message", message
					.substring(Zap02MessageBroadcastReceiver.ZAPPER_KEY_WORD
							.length()));
		}
		return intent;
	}

	public Zap02MessageBroadcastReceiver() {
		this.speechIntent = SpeechService.getSpeechIntent();
	}

	@Override
	public void onReceive(final Context context, final Intent intent) {
		if (Zap02MessageBroadcastReceiver.destructionKey.equals(intent
				.getExtras().getString("message"))) {
			Log.d(Zap02MessageBroadcastReceiver.TAG, "destruction initiated...");
			SpeechService.setPhrase(this.speechIntent, new UKString(
					"Self destruction sequence initiated."));
			context.startService(this.speechIntent);
		} else if (Zap02MessageBroadcastReceiver.haltKey.equals(intent
				.getExtras().getString("message"))) {
			Log.d(Zap02MessageBroadcastReceiver.TAG, "destruction aborted...");
			SpeechService.setPhrase(this.speechIntent, new UKString(
					"Self destruction sequence aborted."));
			context.startService(this.speechIntent);
		} else {
			Log.d(Zap02MessageBroadcastReceiver.TAG, "unknown command.");
		}

	}
}
